﻿$(document).ready(function () {
    //get all elements that position absolute, and move them up to bt in the "publicLayout"
    $(".publicLayout #content .absoluteInDocument").each(function () {
        $(".page.publicLayout").append($(this));
    });

    //check if the user is sleeping
    function checkIsUserSleeping() {
        $.post(pathPrefix + "Users/IsUserSleeping", null, function (result) {
            if (result != null) {

                //user is sleeping
                if (result.Key == true) {
                    var mainDialog = $("#divMainDialog");
                    if ($("#divMainDialog:visible").length == 0) {
                        mainDialog = updateDialog("משתמש לא פעיל", "140", "400");
                    }

                    //show warning message if the user is sleeping
                    var dialogContent = $("<div class='alignCenter'></div>");
                    dialogContent.append("המשתמש שלך לא פעיל יותר מעל 30 דקות.<br />בעוד " + result.Value + " דקות אתה תנותק אוטומטית.");
                    dialogContent.append("<div><input type='button' value='הישאר מחובר' id='btnKeepAlive' class='submitButton' /></div>");

                    mainDialog.empty().append(dialogContent);
                }
                //user is active
                else {
                    closeDialog();
                }

                //every 60 seconds - check again
                window.setTimeout(checkIsUserSleeping, 60000);
            }
        }, "json");
    }
    checkIsUserSleeping();

    $("#btnKeepAlive").live("click", function () {
        //Invoke AJAX method to keep this user alive (by making request to the server)
        $.post(pathPrefix + "Users/KeepAlive", null, function () { }, "json");
        closeDialog();
    });

    function setSelectedMenuItem() {
        //highlight the relevant menu item (according to the current url)
        var currentUrl = document.URL;
        currentUrl = currentUrl.replace('http://', '');
        currentUrl = currentUrl.substring(currentUrl.indexOf('/'), currentUrl.length);

        var selectedItem = $("ul#mainMenu li a[href='" + currentUrl + "'],ul#mainMenu li a[href='" + currentUrl + "/']");
        selectedItem.addClass("hover");
        //selectedItem.each(menuItemMouseOver);
    }

    //when the user get the mouse over menu item - change its image
    function menuItemMouseOver() {
        var img = $(this).find("img");

        var hoverSrc = img.data("hover");
        var originalSrc = img.attr("src");

        img.data("original", originalSrc);
        img.attr("src", hoverSrc);
    }

    //when the user get the mouse out of menu item - change back its image
    function menuItemMouseOut() {
        var img = $(this).find("img");

        var originalSrc = img.data("original");

        img.attr("src", originalSrc);
    }

    setSelectedMenuItem();

    //when hovering on menu item - change its image
    $("ul#mainMenu li").hover(menuItemMouseOver, menuItemMouseOut);

    $("#frmEditorLoginMain").submit(function (e) {
        var url = $(this).find("input[type=submit]").data("url");
        var orderNum = $("#OrderNum").val();

        if (orderNum != "") {
            //Invoke ajax method to check if this order number exists
            $.post(pathPrefix + "Albums/IsOrderNumExists", { "OrderNum": orderNum }, function (result) {
                if (result) {
                    //go to the login page
                    window.location = url + "/" + orderNum;
                }
                else {
                    $("#orderNumNotExists").html("מספר הזמנה לא קיים").show().delay(2000).fadeOut();
                }
            }, "json");
        }
        //the user has not entered order number
        else {
            $("#orderNumNotExists").html("יש להזין מספר הזמנה").show().delay(2000).fadeOut();
        }

        e.preventDefault();
    });
});